using System;
using System.Windows;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace gcpN
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            
            // 检查.NET版本
            if (Environment.Version.Major < 8)
            {
                MessageBox.Show("此应用程序需要.NET 8或更高版本。", 
                    "不兼容的.NET版本", MessageBoxButton.OK, MessageBoxImage.Error);
                this.Shutdown();
                return;
            }

            // 设置全局异常处理
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show($"发生未处理的异常：{e.Exception.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            e.Handled = true;
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show($"发生严重错误：{(e.ExceptionObject as Exception)?.Message ?? "未知错误"}", 
                "严重错误", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}
